package main

import (
	"encoding/csv"
	"fmt"
	"github.com/gogap/logs"
	"os"
	"sync"
	"time"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

func main()  {
	dingo.InitViper("./configs/adm.yaml")
	dbs.NewMysqlComponent().Step()

	file, err := os.Open("./dll20191220.csv")  //读取文件
	//file, err := os.Open("./cmd/test/dll20191220.csv")  //读取文件
	if err != nil {
		fmt.Println(err)
	}

	defer file.Close()

	reader := csv.NewReader(file)
	reader.FieldsPerRecord = -1
	csvdata, err := reader.ReadAll()   //读取全部数据

	var wg sync.WaitGroup
	for idx, line := range csvdata {    //按行打印数据
		if idx > 0 {
			key := line[0]
			wg.Add(1)
			go func(key string, lst []string) {
				defer wg.Done()
				for i := 1; i <= 50; i++ {
					t := "A"
					if i == 50 {
						t = "Z"
					}
					db := global.Orm.Exec("INSERT INTO `ble_device_password_dll`(`index`, `key`, `type`, `password`, `update_time`) VALUES (?, ?, ?, ?, ?);", i, key, t, lst[i], time.Now().Format("2006-01-02 15:04:05"))
					if db.Error != nil {
						logs.Info("服务器内部错误：", db.Error.Error(), i, time.Now().Format("2006-01-02 15:04:05"))
						return
					}
				}
			}(key, line)
		}
	}
	wg.Wait()

	time.Sleep(10*time.Second)

}
